package com.huohuzhihui.web.controller.merchant;

import com.huohuzhihui.common.annotation.Log;
import com.huohuzhihui.common.config.RuoYiConfig;
import com.huohuzhihui.common.core.controller.BaseController;
import com.huohuzhihui.common.core.domain.AjaxResult;
import com.huohuzhihui.common.core.domain.entity.SysUser;
import com.huohuzhihui.common.core.domain.model.LoginUser;
import com.huohuzhihui.common.core.page.TableDataInfo;
import com.huohuzhihui.common.enums.BusinessType;
import com.huohuzhihui.common.utils.StringUtils;
import com.huohuzhihui.common.utils.file.FileUploadUtils;
import com.huohuzhihui.common.utils.file.MimeTypeUtils;
import com.huohuzhihui.common.utils.poi.ExcelUtil;
import com.huohuzhihui.framework.config.ServerConfig;
import com.huohuzhihui.framework.web.service.TokenService;
import com.huohuzhihui.merchant.domain.MerCoupon;
import com.huohuzhihui.merchant.domain.MerCouponGet;
import com.huohuzhihui.merchant.domain.MerResource;
import com.huohuzhihui.merchant.service.IMerCouponGetService;
import com.huohuzhihui.merchant.service.IMerCouponService;
import com.huohuzhihui.merchant.service.IMerResourceService;
import com.huohuzhihui.task.domain.PublishTask;
import com.huohuzhihui.task.service.IPublishTaskService;
import com.huohuzhihui.task.service.ITaskDyPublishService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商户资源Controller
 *
 * @author huohuzhihui
 * @date 2021-04-01
 */
@RestController
@RequestMapping("/merchant/resource")
public class MerResourceController extends BaseController {
    @Autowired
    private IMerResourceService merResourceService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private ServerConfig serverConfig;

    @Autowired
    private IPublishTaskService publishTaskService;

    @Autowired
    private ITaskDyPublishService taskDyPublishService;

    @Autowired
    private IMerCouponService merCouponService;

    @Autowired
    private IMerCouponGetService merCouponGetService;


    /**
     * 查询商户资源列表
     */
    @PreAuthorize("@ss.hasPermi('merchant:resource:list')")
    @GetMapping("/list")
    public TableDataInfo list(MerResource merResource) {
        startPage();
        List<MerResource> list = merResourceService.selectMerResourceList(merResource);
        return getDataTable(list);
    }

    /**
     * 导出商户资源列表
     */
    @PreAuthorize("@ss.hasPermi('merchant:resource:export')")
    @Log(title = "商户资源", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MerResource merResource) {
        List<MerResource> list = merResourceService.selectMerResourceList(merResource);
        ExcelUtil<MerResource> util = new ExcelUtil<MerResource>(MerResource.class);
        return util.exportExcel(list, "resource");
    }

    /**
     * 获取商户资源详细信息
     */
    @PreAuthorize("@ss.hasPermi('merchant:resource:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(merResourceService.selectMerResourceById(id));
    }

    /**
     * 新增商户资源
     */
    @PreAuthorize("@ss.hasPermi('merchant:resource:add')")
    @Log(title = "商户资源", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MerResource merResource) {
        //判断是否有多个文件
        String video = merResource.getResourceUrl();
        String[] videoArrays = video.split(",");
        for (int i = 0; i < videoArrays.length; i++) {
            String videoUrl = videoArrays[i];
            if (StringUtils.isNotEmpty(videoUrl) && !"null".equals(videoUrl)) {
                MerResource resource = new MerResource();
                resource.setResourceUrl(videoUrl);
                resource.setGroupId(merResource.getGroupId());
                resource.setMerchantId(merResource.getMerchantId());
                merResourceService.insertMerResource(resource);
            }
        }
        return AjaxResult.success();
    }

    /**
     * 修改商户资源
     */
    @PreAuthorize("@ss.hasPermi('merchant:resource:edit')")
    @Log(title = "商户资源", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MerResource merResource) {
        return toAjax(merResourceService.updateMerResource(merResource));
    }

    /**
     * 删除商户资源
     */
    @PreAuthorize("@ss.hasPermi('merchant:resource:remove')")
    @Log(title = "商户资源", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(merResourceService.deleteMerResourceByIds(ids));
    }

    /**
     * 可用的视频数
     */
    @PreAuthorize("@ss.hasPermi('merchant:resource:remove')")
    @Log(title = "商户资源", businessType = BusinessType.DELETE)
    @GetMapping("/getMaxSend")
    public AjaxResult getMaxSend(Long merchantId)
    {
        //查询视频总数
        MerResource merResource = new MerResource();
        merResource.setMerchantId(merchantId);
        merResource.setGroupId(10L);
        List g1 = this.merResourceService.selectMerResourceList(merResource);
        int count1 = g1.size();

        merResource.setGroupId(11L);
        List g2 = this.merResourceService.selectMerResourceList(merResource);
        int count2 = g2.size();

        merResource.setGroupId(12L);
        List g3 = this.merResourceService.selectMerResourceList(merResource);
        int count3 = g3.size();

        merResource.setGroupId(13L);
        List g4 = this.merResourceService.selectMerResourceList(merResource);
        int count4 = g4.size();

        merResource.setGroupId(14L);
        List g5 = this.merResourceService.selectMerResourceList(merResource);
        int count5 = g5.size();




        PublishTask task = new PublishTask();
        task.setMerchantId(merchantId+"");
        task.setStatus(2);
        List send = publishTaskService.selectPublishTaskList(task);
        if(send==null){
            send = new ArrayList();
        }
        int maxSend = 0;
        if(count1==0 && count2==0 && count3==0 & count4==0 && count5==0){
            maxSend = 0;
        }else{
            if(count1==0){
                count1 = 1;
            }
            if(count2==0){
                count2 = 1;
            }
            if(count3==0){
                count3 = 1;
            }
            if(count4==0){
                count4 = 1;
            }
            if(count5==0){
                count5 = 1;
            }
            maxSend = count1 * count2 * count3 * count4 * count5 - send.size();
        }

        //剩余可播放数
        Map<String,Object> result = new HashMap<>();
        result.put("max",maxSend);
        //累计参与数
        long canyu = publishTaskService.getJoinCount(task);
        result.put("canyu",canyu);
        //累计播放数
        long bofang = taskDyPublishService.getPlayCount(task);
        result.put("bofang",bofang);
        //累计点赞数
        long dianzan = taskDyPublishService.getDiggCount(task);
        result.put("dianzan",dianzan);
        //累计评论数
        long pinglun = taskDyPublishService.getCommentCount(task);
        result.put("pinglun",pinglun);


        //查询优惠券核销数量
        MerCouponGet couponGet = new MerCouponGet();
        couponGet.setMerchantId(merchantId);
        couponGet.setUseStatus(1);
        Long checkCount = this.merCouponGetService.selectMerCouponCheckCount(couponGet);
        result.put("checkCount",checkCount);
        return AjaxResult.success(result);
    }

}
